<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>东财个股人气榜</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			window.onload = function() {
			getSource1();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource();					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				//time_range("9:00","15:15")
				getSource1();						
				},
				60000);
			})
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}						
									
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();
				} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}				
			}
			
			function getSource1() {	    							
				var url = "https://emappdata.eastmoney.com/stockrank/getAllCurrentList"; 	                			
				createXmlHttp();
				var array = {"appId":"appId01","globalId":"786e4c21-70dc-435a-93bb-38","pageNo":1,"pageSize":100}
				xmlHttp.onreadystatechange = writeSource1;
				xmlHttp.open("POST", url, true);
				xmlHttp.setRequestHeader("Content-Type","application/json");
				xmlHttp.withCredentials = true
				//xmlHttp.send(null);
				xmlHttp.send(JSON.stringify(array)) ; 
			}
								
			var dms = "";
			function writeSource1() { 
			if (xmlHttp.readyState == 4) {         
				var data = xmlHttp.responseText	
				//document.write(data);
				 ss = data.split('},{');				
				for (i = 0; i < ss.length; i++)
				{
				//alert(ss[i]);
					dms += ss[i].match(/\"sc\":\"(\S*)\",/)[1].replace("SH","1.").replace("SZ","0.") + ",";
				
				}
				dms = dms.substring(0,dms.length - 1);
				//document.write(dms);
				getSource2();				
				}
			}
			
			function getSource2() {	    							
				var url = "https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&secids=" + dms + "&fields=f2,f3,f12,f14"; 	                							
				createXmlHttp();				
				xmlHttp.onreadystatechange = writeSource2;
				xmlHttp.open("GET", url, true);				
				//xmlHttp.withCredentials = true
				xmlHttp.send(null);				
			}
						
			function writeSource2() { 
			if (xmlHttp.readyState == 4) {         
				var data = xmlHttp.responseText	
				//document.write(data);
				var transform_aa = [           
				{"tag":"ul", children:function(){return(json2html.transform(this.data, transform_bb));}}
				];
				var transform_bb = [           
				{"tag":"ul", children:function(){return(json2html.transform(this.diff, transform_cc));}}
				];
				
				m = 0;
				var transform_cc= 				
				{"tag":"ul", children:[	
				{"tag":"li", children:function(){
					m++;				
					return(
					"<a href=http://www.treeid/code_" +
						this.f12 + ">" + m + ". " + 						
						this.f12 + "&nbsp;&nbsp;" + 
						this.f14 + "&nbsp;&nbsp;" +
						"现价：" + this.f2 + "&nbsp;&nbsp;" +
						"涨幅：" + this.f3 + "%" + 
						 "</a>" 					
					);
				}}				
				//{"tag": "li", "html":"<a href='http://www.treeid/code_${f12}' >${f12}&nbsp;&nbsp;${f14}&nbsp;&nbsp;现价：${f2}&nbsp;&nbsp;涨幅：${f3}% </a>"}, 							
				]};  
				
				var html = json2html.transform(data, transform_aa);		
				div = document.getElementById('towrite');
				var myDate = new Date();
				div.innerHTML = '<h2>东财个股人气榜&nbsp;&nbsp;&nbsp;&nbsp;' + myDate.Format("hh:mm:ss") + '</h2>' + html;
				}
			}
		</script>
	</head>
	<body>
		<div id="towrite"></div>
	</body>
</html>